layui.use(['form','layer','table','laytpl','util'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table,
        util = layui.util;

    // 动态生成组列表
    $.get("/deploy/queryGruopList",function(data){
        $.each(data, function(index, item){
            $(".groupSelector").append(new Option(item.id, item.id));
        });
        $(".groupSelector").val(data[0].id);
        form.render();
    },"json");

    // 任务列表
    var tableIns = table.render({
        elem: '#groupTaskList',
        url : '/deploy/queryTaskByGroup',
        cellMinWidth : 65,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "groupTaskListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'name', title: '任务名称', minWidth:100, align:"center"},
            {field: 'createTime', title: '创建时间', minWidth:100, align:"center", templet:function (d) {
                return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss");
            }},
            {field: 'assignee', title: '状态', minWidth:100, align:"center", templet:function (d) {
                return "<span style='color:#FF5722'>待拾取</span>";
            }},
            {title: '操作', minWidth:175, templet:'#groupTaskListBar', fixed:"right", align:"center"}
        ]]
    });

    // 监听select即用户组的选择
    form.on('select(groupSelector)', function(data){
        table.reload("groupTaskListTable",{
            url: "/deploy/queryTaskByGroup",
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                groupname: data.value
            }
        })
    });

    //列表操作
    table.on('tool(groupTaskList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'take'){ //拾取
            take(data);
        }else if(layEvent === 'showImg'){ //查看流程图
            showImg(data);
        }else if(layEvent === 'del'){ //删除
            layer.msg("暂未实现此功能");
        }
    });

    // 拾取
    function take(edit) {
        layer.confirm('确定拾取选中的任务吗？', {icon: 3, title: '提示信息'}, function (index) {
            $.get("/deploy/takeTask",{
                taskId : edit.id
            },function(data){
                //成功回调函数
                layer.close(index);
                var index2 = layer.msg('拾取中，请稍候',{icon: 16,time:false,shade:0.8});
                table.reload("groupTaskListTable",{
                    url: "/deploy/queryTaskByGroup",
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        groupname: $(".groupSelector").val()
                    }
                });
                setTimeout(function(){
                    layer.close(index2);
                    layer.msg(data.msg);
                },250);
            });
        })
    }

    // 展示流程图
    function showImg(data) {
        var index = layui.layer.open({
            title: "流程图",
            type: 2,
            content: "/deploy/viewCurrentImageByTaskId?taskId="+data.id,
            area: ['780px', '560px'],
            success: function (layero, index) {
                setTimeout(function () {
                    layui.layer.tips('点击此处返回任务列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
    }
});